package io.renren.modules.generator.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import com.alibaba.fastjson.JSONArray;
import io.renren.common.utils.ExportExcel;
import io.renren.common.utils.ImportExcelUtil;
import io.renren.modules.generator.entity.*;
import io.renren.modules.generator.entity.vo.DecideInfoVo;
import io.renren.modules.generator.entity.vo.TTrainingScoreVo;
import io.renren.modules.generator.entity.vo.TTrainingVo;
import io.renren.modules.generator.service.TObjectService;
import io.renren.modules.generator.service.TTrainingObjectService;
import io.renren.modules.user.entity.TPeopleEntity;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.ibatis.jdbc.Null;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.generator.service.DecideInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-08-16 10:05:26
 */
@RestController
@RequestMapping("/decideinfo")
public class DecideInfoController {
    @Autowired
    private DecideInfoService decideInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("generator:decideinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = decideInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("generator:decideinfo:info")
    public R info(@PathVariable("id") Integer id){
		DecideInfoEntity decideInfo = decideInfoService.getById(id);

        return R.ok().put("decideInfo", decideInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("generator:decideinfo:save")
    public R save(@RequestBody DecideInfoEntity decideInfo){
		decideInfoService.save(decideInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("generator:decideinfo:update")
    public R update(@RequestBody DecideInfoEntity decideInfo){
		decideInfoService.updateById(decideInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("generator:decideinfo:delete")
    public R delete(@RequestBody Integer[] ids){
		decideInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    //导出
    @RequestMapping("/exports")
   // @ResponseBody
    @ApiOperation(value = "导出报表", httpMethod = "GET", notes = "导出报表,前端写一个导出地址调用此接口即可")
    public R exportLyHj(HttpServletResponse response) throws IOException {
        //DecideInfoEntity trainingEntity=new DecideInfoEntity();
        //trainingEntity.setId(1);
        DecideInfoEntity decideInfo=new DecideInfoEntity();
        List<DecideInfoVo> voList = decideInfoService.getDecideInfoEntity(decideInfo);
        Logger.getLogger("获取全部列表数据"+JSONArray.toJSON(voList));
        String title = "考核判定";
        String[] rowsName = new String[]{"序号", "科目名称", "性别","条件上线","条件下线","条件上线","条件下线","成绩","判定"};
        List<Object[]>  dataList = new ArrayList<>();
        Object[] objs = null;
        for (int i = 0; i < voList.size(); i++) {
            DecideInfoVo signupInfo= voList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = signupInfo.getId();
            objs[1] = signupInfo.getName();
            if(signupInfo.getGender()=="0"){
                objs[2] = "男";
            }
            if(signupInfo.getGender()=="1"){
                objs[2] = "女";
            }
            objs[3]=signupInfo.getConditionA();
            objs[4]=signupInfo.getConditionB();
            objs[5]=signupInfo.getConditionC();
            objs[6]=signupInfo.getConditionD();
            objs[7]=signupInfo.getScore();
            objs[8]=signupInfo.getDecide();
            dataList.add(objs);
        }
        try{
            ExportExcel ex = new ExportExcel(title, rowsName, dataList);
            ex.export();
        }catch(Exception e){
            System.out.println("Wrong!");
        }
        return R.ok().put("dataList",dataList);

    }

    /**
     * 倒入
     */
    int objectId=0; //科目id
    @RequestMapping("/asd")
    @ResponseBody
    public R imputTestdd(@RequestBody DecideInfoEntity decideInfoEntity){
        ImportExcelUtil importExcelUtil=new ImportExcelUtil();
        decideInfoEntity.setFile("C:\\Users\\HuiHui\\Desktop\\判定.xls");
        File url=null;
        if (decideInfoEntity.getFile()!=null){
            url = new File(decideInfoEntity.getFile());
        }
        List<List<Object>> dataList= null;
        List<DecideInfoVo> list=new ArrayList<>();
        try {
            dataList = importExcelUtil.importExcel(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //数据封装格式一，将表格中的数据遍历取出后封装进对象放进List
        Object score;  //成绩
        Object decide;  //判定
        Object objectName; //科目名称
        Object gender;   //性别
        Object conditionA;  //条件上线
        Object conditionB;  //条件下线
        Object conditionC;  //条件上线
        Object conditionD;  //条件下线
            for (int i = 1; i <dataList.size(); i++) {
                objectName=( dataList.get(i).get(1));   //科目名称
                TObjectEntity obj=new TObjectEntity();
                obj.setName((String) objectName);
                getKmId(obj);
                gender =(dataList.get(i).get(2));   //性别
//                if(gender=="男"){
//                    gender=0;
//                }
//                if(gender=="女"){
//                    gender=1;
//                }
                conditionA=(dataList.get(i).get(3));  //条件上线
                conditionB=(dataList.get(i).get(4));  //条件下线
                conditionC=(dataList.get(i).get(5));  //条件上线
                conditionD=(dataList.get(i).get(6));  //条件下线
                score=(dataList.get(i).get(7));  //成绩
                decide=(dataList.get(i).get(8));  //判定
                if(i<=0){

                }else{
                    DecideInfoVo signupCategory=new DecideInfoVo();
                    signupCategory.setObjectId(objectId);
                    signupCategory.setGender((String) gender);
                    signupCategory.setConditionA((String) conditionA);
                    signupCategory.setConditionB((String) conditionB);
                    signupCategory.setConditionC((String) conditionC);
                    signupCategory.setConditionD((String) conditionD);
                    signupCategory.setScore((String)score);
                    signupCategory.setDecide((String)decide);
                    list.add(signupCategory);
                }
            }
        System.out.println(list.size());
        decideInfoService.getDrDecideInfo(list);
        return R.ok().put("list",list);
    }

    @Autowired
    private TObjectService objectService;

    /**
     * 获取id
     * @return
     */
    public R getKmId(TObjectEntity obj){
        TObjectEntity s=objectService.getObjId(obj);
        if(s==null){
            return R.ok();
        }
        System.out.println(s.getId());
        objectId=s.getId();
        return R.ok();
    }
}
